<?php

namespace App\Http\Controllers\Wx;


use App\CodeResponse;
use App\Models\User\Address;
use App\Services\User\AddressServices;
use Illuminate\Http\Request;
use Illuminate\Support\Str;


class AddressController extends WxController
{

    protected $only= ['list','save','detail','delete'];
    /**
     * 获取用户地址列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function list()
    {
        $list = AddressServices::getInstance()->getAddressListByUserId($this->user()->id);
        return $this->successPaginate($list);
    }

    public function detail(Request $request)
    {
        $id = $request->input('id');
        $address = AddressServices::getInstance()->getUserAddress($id)->toArray();
        $item = [];
        foreach ($address as $key => $value) {
            $key = lcfirst(Str::studly($key));
            $item[$key] = $value;
        }
        return $this->success($item);
    }

    public function save(Request $request)
    {
        $data = $request->all();
        $id = $request->input('id');
        $user = $this->user();
        $address = new Address();
        if ($data['isDefault']) {
            $updateDefault = $address->query()->where('is_default', 1)->where('user_id', $user->id)->first();
            if (!is_null($updateDefault)) {
                $updateDefault->is_default = false;
                $updateDefault->save();
            }
        }
        if (!is_null($id)) {
            $address = $address->query()->find($id);
            $address->name = $data['name'];
            $address->tel = $data['tel'];
            $address->province = $data['province'];
            $address->city = $data['city'];
            $address->county = $data['county'];
            $address->area_code = $data['areaCode'];
            $address->postal_code = $data['postalCode'];
            $address->address_detail = $data['addressDetail'];
            $address->is_default = $data['isDefault'];
            $address->user_id = $user->id;
            $ret = $address->save();
        } else {
            $address->name = $data['name'];
            $address->tel = $data['tel'];
            $address->province = $data['province'];
            $address->city = $data['city'];
            $address->county = $data['county'];
            $address->area_code = $data['areaCode'];
            $address->postal_code = $data['postalCode'];
            $address->address_detail = $data['addressDetail'];
            $address->is_default = $data['isDefault'];
            $address->user_id = $user->id;
            $ret = $address->save();
        }
        return $this->failOrSuccess($ret, CodeResponse::UPDATED_FAIL);
    }


    /**
     * 地址删除
     * @param  Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {
        $id = $request->input('id');
        if (empty($id) && !is_numeric($id)) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        AddressServices::getInstance()->delete($this->user()->id, $id);
        return $this->success();
    }
}
